package com.yuntongxun.ecdemo.voicechat.ui.activity;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yuntongxun.ecdemo.R;
import com.yuntongxun.ecdemo.voicechat.bean.PostResult;
import com.yuntongxun.ecdemo.voicechat.comman.EventBusTags;

import java.io.IOException;
import java.io.InputStream;

import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 注册页面1-选择国家
 * @author li_zhuonan
 * @version 1.0
 * @since 2016/11/15
 */
public class RegisterFlagActivity extends BaseChatActivity {

    private static final int FLAG_REQUEST_CODE=1;
    @InjectView(R.id.country_img)
    ImageView flagImg;
    @InjectView(R.id.country_name_tv)
    TextView flagNameTv;

    public static final String FLAG="flag";

    @Override
    protected boolean isBindEventBusHere() {
        return true;
    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.activity_voice_chat_register1;
    }

    @Override
    protected void initViewsAndEvents() {
        setTitle("Register");
    }

    @OnClick(R.id.country_select_layout)
    public void onSelectClick(){
        Intent intent=new Intent(this,FlagSelectActivity.class);
        startActivityForResult(intent,FLAG_REQUEST_CODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==RESULT_OK&&data!=null){
            switch (requestCode){
                case FLAG_REQUEST_CODE:
                    String flagName=data.getStringExtra(FlagSelectActivity.FLAG_NAME);
                    Log.e("flag",flagName+"");
                    try {
                        InputStream is=getAssets().open("flag/"+flagName+".png");
                        flagImg.setVisibility(View.VISIBLE);
                        flagImg.setImageBitmap(BitmapFactory.decodeStream(is));
                        flagNameTv.setText(flagName);
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    }

    @OnClick(R.id.next_btn)
    public void onNextClick(){

        if("Choose country".equals(flagNameTv.getText().toString())){
            showToastShort("Please choose your country!");
            return;
        }

        Intent intent=new Intent(this,RegisterGenderActivity.class);
        intent.putExtra(FLAG,flagNameTv.getText().toString().trim());
        startActivity(intent);
    }

    public void onEvent(PostResult postResult){
        if (EventBusTags.REGISTER_FINISH.equals(postResult.getTag())){
            finish();
        }
    }

}
